
package kaizone.haining.chaobohui.setup;

import kaizone.haining.chaobohui.json.VersionDO;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class APPAbout {

    public static final String CHECK_FOR_UPDATE = "http://static.dp-app.com/download/update.json";

    public Context mContext;

    public APPAbout(Context context) {
        mContext = context;
    }

    public int getVersionCode() {
        int versionCode = 0;
        try {
            PackageManager pm = mContext.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(mContext.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public void check(VersionDO versionDO) {
        if (versionDO == null)
            return;
        int lastversion = versionDO.getVersion1(VersionDO.ANDROID);
        int appversion = getVersionCode();
        promptUpate(lastversion, appversion, versionDO);
    }

    public void promptVersion(int version) {
        StringBuffer sb = new StringBuffer();
        sb.append("\t\t").append("当前版本为：").append(version).append("\t");
        sb.append("为最新版本，无需更新");
        new AlertDialog.Builder(mContext).setTitle("检查更新").setMessage(sb.toString())
                .setPositiveButton("确定", null).show();
    }

    public void promptVersionUpdate(int appversion, int latestverion, VersionDO versionDO) {
        StringBuffer sb = new StringBuffer();
        sb.append("\t\t").append("当前版本为：").append(appversion).append("\t");
        sb.append("最新版本为：").append(latestverion);
        sb.append("是否更新？");
        new AlertDialog.Builder(mContext).setTitle("检查更新").setMessage(sb.toString())
                .setPositiveButton("确定", new AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).setNegativeButton("取消", null).show();
    }

    public void promptUpate(final int latestverion, final int appversion, final VersionDO versionDO) {
        if (latestverion > appversion) {
            promptVersionUpdate(appversion, latestverion, versionDO);
        } else {
            promptVersion(appversion);
        }
    }

    public void upate(VersionDO versionDO) {

    }
}
